package com.blb.ssm.shiro;

import java.util.ArrayList;
import java.util.List;

import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.util.ByteSource;
import org.springframework.beans.factory.annotation.Autowired;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.blb.ssm.entity.Menu;
import com.blb.ssm.entity.Role;
import com.blb.ssm.entity.RoleMenu;
import com.blb.ssm.entity.User;
import com.blb.ssm.entity.UserRole;
import com.blb.ssm.service.IMenuService;
import com.blb.ssm.service.IRoleMenuService;
import com.blb.ssm.service.IRoleService;
import com.blb.ssm.service.IUserRoleService;
import com.blb.ssm.service.IUserService;

public class MyRealm extends AuthorizingRealm {
	
	@Autowired
	private IUserService userService;
	
	@Autowired
	private IUserRoleService userRoleService;
	
	@Autowired
	private IRoleMenuService roleMenuService;
	
	@Autowired
	private IRoleService roleService;
	
	@Autowired
	private IMenuService menuService;

	@Override
	protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
		System.out.println("授权");
		String username = (String) principalCollection.getPrimaryPrincipal();
		User user2 = new User();
		user2.setUserName(username);
		User user = userService.getOne(new QueryWrapper<User>(user2));
		
		//角色列表
		UserRole userRole2 = new UserRole();
		userRole2.setUserId(user.getId());
		List<UserRole> UserroleList = userRoleService.list(new QueryWrapper<UserRole>(userRole2));
		
		List<String> roleIds = new ArrayList<String>();
		
		SimpleAuthorizationInfo simpleAuthorizationInfo = new SimpleAuthorizationInfo();
		for (UserRole Userrole : UserroleList) {
			roleIds.add(Userrole.getRoleId());
			Role role = roleService.getById(Userrole.getRoleId());
			if (role!=null) {
				simpleAuthorizationInfo.addRole(role.getRole());
			}
		}
		
		//权限列表
		if (!roleIds.isEmpty()) {
			
			RoleMenu roleMenu2 = new RoleMenu();
			QueryWrapper<RoleMenu> queryWrapper = new QueryWrapper<RoleMenu>(roleMenu2);
			queryWrapper.in("role_id", roleIds);
			
			List<RoleMenu> list = roleMenuService.list(queryWrapper);
			
			for (RoleMenu Rolemenu : list) {
				Menu menu = menuService.getById(Rolemenu.getMenuId());
				if (menu!=null) {
					simpleAuthorizationInfo.addStringPermission(menu.getPermiss());
				}
			}
		}
		return simpleAuthorizationInfo;
	}

	@Override
	protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
		UsernamePasswordToken token = (UsernamePasswordToken) authenticationToken;
		String username = token.getUsername();
		User user = userService.getOne(new QueryWrapper<User>().eq("user_name", username));
		if (user==null) {
			return null;
		}
		ByteSource credentialsSalt = ByteSource.Util.bytes(username + user.getSalt());
		SimpleAuthenticationInfo info = new SimpleAuthenticationInfo(username, user.getPassword(), credentialsSalt, getName());
		return info;
	}

}
